<?php
/**
 * Created by PhpStorm.
 * User: maqun
 * Date: 2016/2/29
 * Time: 12:35
 * 新闻管理
 */
class NewsAction extends BaseAction{

    protected $page_size = 10;

    public function _initialize(){
        self::$action = 1;
        parent::_initialize();
    }

    /**
     * 新闻列表
     */
    public function index(){

        vendor('Page');
        $con   = I('get.condition');
        $where = !empty($con) ? " locate(title,'{$con}')" : '';
        $count = D('News')->where($where)->count();

        $page  = new Page($count,$this->page_size);

        $list = D('News')->where($where)
            ->order('time DESC')
            ->limit($page->firstRow.','.$page->listRows)
            ->select();

        $this->assign('condition',$con);
        $this->assign('page',$page);
        $this->assign('list',$list);

        $this->display('index');
    }

    public function add(){

        $this->display('add');
    }
    public function edit(){
        $get = $this->_get('id');
        $content = D('News')->where("news_id = $get")->select();

        $this->assign('content',$content[0]);
        $this->display('edit');

    }
    public function delete(){
         $get = $this->_get('id');
         $data = array('defunct' => 'Y');

        $News = D('News')->where("news_id = $get")->save($data);
    }   
    public function save(){
        $post = $this->_post();

        if(!empty($post)){
            $post["time"] = date("Y-m-d H:i:s");

            if(!empty($post['id'])){
                $News = D('News')->where("news_id = $post[id]")->save($post);
            }else{
                $post["user_id"] = $_SESSION['USER']['user_id'];
                $News = D('News')->add($post);
            }
        }
        header("Location: index");
    } 
}